/*
 * ﻿Copyright (C) 2009-2013 NewMain Softech
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
package com.newmainsoftech.gwetons.client;

import java.util.LinkedHashSet;

import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.NodeList;

public class MetaTagCrawler {
	/**
	 * @param metaName to specify value of name attribute of meta tag. It's case sensitive.
	 * @return value(s) of content attribute of meta tag(s). <br />
	 * If name and content attributes are not typed with lower cases, then this will not pick up 
	 * that meta tag. 
	 * i.e. wrong: &lt;meta <b>Name</b>=&quot;testName&quot; <b>Content</b>=&quot;testContent&quot; /&gt;, 
	 * correct: &lt;meta <b>name</b>=&quot;testName&quot; <b>content</b>=&quot;testContent&quot; /&gt;
	 * @throws IllegalArgumentException
	 */
	static public LinkedHashSet<String> getContents( String metaName) throws IllegalArgumentException {
		if ( ( metaName == null) || "".equals( metaName)) {
			throw new IllegalArgumentException( "");
		}

		LinkedHashSet<String> contentLinkedHashSet = new LinkedHashSet<String>();
		
		NodeList<Element> metaTagNodeList = Document.get().getElementsByTagName( "meta");
		for( int metaTagIndex = 0; metaTagIndex < metaTagNodeList.getLength(); metaTagIndex++) {
			Element metaTagElement = metaTagNodeList.getItem( metaTagIndex);
			if ( !metaName.equals( metaTagElement.getAttribute( "name"))) continue; // for
			String content = metaTagElement.getAttribute( "content");
			if ( ( content == null) || "".equals( content))continue; // for
			contentLinkedHashSet.add( content);
		} // for
		
		return contentLinkedHashSet;
	} // static public LinkedHashSet<String> getContents( String metaName) throws IllegalArgumentException
}
